package com.one.group.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.one.group.model.dto.AdvertData;
import com.one.group.admin.excel.AdvertDataReadListener;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.AdvertAddDto;
import com.one.group.model.dto.AdvertQueryDto;
import com.one.group.model.dto.AdvertUpdateDto;
import com.one.group.model.entity.Advert;
import com.one.group.model.vo.AdvertListVo;
import com.one.group.result.Result;
import com.one.group.service.AdvertService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * @Author wanbo
 * @Date 2025/9/28 16:49
 */
@RestController
@RequestMapping("/advert")
@Slf4j
@Tag(name = "广告管理相关接口")
public class AdvertController {

    @Autowired
    private AdvertService advertService;

    @PostMapping("/upload")
    @Operation(summary = "上传广告")
    public Result uploadExcel(@RequestParam("file") MultipartFile file, Long cellId) throws Exception {
        // 创建监听器实例
        AdvertDataReadListener listener = new AdvertDataReadListener(advertService, cellId);

        if (cellId==null) {
            log.error("文件：{}、cellId：{},参数不能为空", file, cellId);
            return ResultUtil.error(40000, "参数不能为空");
        }

        // 使用 EasyExcel 读取文件（指定 sheet 和监听器）
        EasyExcel.read(file.getInputStream(), AdvertData.class, listener)
                .sheet(0) // 读取第一个 sheet
                .doRead(); // 触发解析

        // 解析完成后可对数据进行后续处理（如存库、返回结果等）
        return ResultUtil.success();
    }

    @GetMapping("/findAdvertById")
    @Operation(summary = "根据id查询广告")
    public Result findAdvertById(String id){
        Advert advert = advertService.findAdvertById(id);
        return ObjectUtil.isEmpty(advert) ? ResultUtil.error(ErrorCode.NOT_FOUND_ERROR):ResultUtil.success(advert);
    }

    @PostMapping("/findAdvertByQueryDto")
    @Operation(summary = "根据条件分页查询")
    public Result findAdvertByQueryDto(@RequestBody AdvertQueryDto advertQueryDto){
        AdvertListVo advertListVo = advertService.findAdvertByQueryDto(advertQueryDto);
        return ResultUtil.success(advertListVo);
    }

    @PostMapping("/inertAdvert")
    @Operation(summary = "添加广告")
    public Result inertAdvert(@RequestBody AdvertAddDto advert){
        advertService.inertAdvert(advert);
        return ResultUtil.success();
    }

    // 此接口只能用于管理员修改  只修改广告位置
    @PostMapping("/updateAdvert")
    @Operation(summary = "更新广告")
    public Result updateAdvert(@RequestBody AdvertUpdateDto advert){
        advertService.updateAdvert(advert);
        return ResultUtil.success();
    }

    @GetMapping("/deleteById")
    @Operation(summary = "根据id删除广告")
    public Result deleteById(String id){
        advertService.deleteAdvertById(id);
        return ResultUtil.success();
    }

    @PostMapping("/deleteBatch")
    @Operation(summary = "批量删除广告")
    public Result deleteBatch(@RequestBody List<String> ids){
        advertService.deleteBatch(ids);
        return ResultUtil.success();
    }

}
